/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package app.view.gui;

import app.entities.model.DTO.MatriculaAlunoDTO;
import app.entities.model.DTO.MatriculaProfessorDTO;
import app.view.classes.ViewTurma;
import app.view.gui.listar.ListaAlunos;
import app.view.gui.listar.ListaProfessores;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Murilo
 */
public class CadastrarTurma extends javax.swing.JFrame {

    private ViewTurma viewTurma;
    private MatriculaProfessorDTO ProfessorResponsavel;
    private ArrayList<MatriculaAlunoDTO> AlunosMatriculadosNaTurma;

    public CadastrarTurma() {
        this.viewTurma = new ViewTurma();
        this.ProfessorResponsavel = new MatriculaProfessorDTO();
        this.AlunosMatriculadosNaTurma = new ArrayList<>();
        initComponents();
        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        pack();
        setLocation((screenSize.width/2) - (getWidth()/2), (screenSize.height/2) - (getHeight()/2));
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLayeredPane1 = new javax.swing.JLayeredPane();
        edtNomeMateria = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLayeredPane2 = new javax.swing.JLayeredPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblProfessor = new javax.swing.JTable();
        btnAdicionaProfessor = new javax.swing.JButton();
        btnCancelarProfessor = new javax.swing.JButton();
        jLayeredPane3 = new javax.swing.JLayeredPane();
        btnPesquisaAluno = new javax.swing.JButton();
        btnIserirAluno = new javax.swing.JButton();
        btnCadastrarTurma = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        tblAlunos = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Cadastrar Turma");

        jLayeredPane1.setBorder(javax.swing.BorderFactory.createTitledBorder("Dados da Turma"));
        edtNomeMateria.setBounds(100, 30, 280, 20);
        jLayeredPane1.add(edtNomeMateria, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel1.setText("Nome da Matéria");
        jLabel1.setBounds(10, 30, 100, 14);
        jLayeredPane1.add(jLabel1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLayeredPane2.setBorder(javax.swing.BorderFactory.createTitledBorder("Professor"));

        tblProfessor.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Id", "Matrícula", "Nome", "Sobrenome"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(tblProfessor);
        tblProfessor.getColumnModel().getColumn(1).setPreferredWidth(20);

        jScrollPane1.setBounds(10, 30, 452, 90);
        jLayeredPane2.add(jScrollPane1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        btnAdicionaProfessor.setIcon(new javax.swing.ImageIcon(getClass().getResource("/app/view/gui/imgs/addpro.png"))); // NOI18N
        btnAdicionaProfessor.setText("Adicionar Professor");
        btnAdicionaProfessor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAdicionaProfessorActionPerformed(evt);
            }
        });
        btnAdicionaProfessor.setBounds(490, 30, 170, 40);
        jLayeredPane2.add(btnAdicionaProfessor, javax.swing.JLayeredPane.DEFAULT_LAYER);

        btnCancelarProfessor.setIcon(new javax.swing.ImageIcon(getClass().getResource("/app/view/gui/imgs/cancelarprof.png"))); // NOI18N
        btnCancelarProfessor.setText("Excluir Professor");
        btnCancelarProfessor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarProfessorActionPerformed(evt);
            }
        });
        btnCancelarProfessor.setBounds(490, 80, 170, 41);
        jLayeredPane2.add(btnCancelarProfessor, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLayeredPane3.setBorder(javax.swing.BorderFactory.createTitledBorder("Alunos"));

        btnPesquisaAluno.setIcon(new javax.swing.ImageIcon(getClass().getResource("/app/view/gui/imgs/addpro.png"))); // NOI18N
        btnPesquisaAluno.setText("Adicionar Aluno");
        btnPesquisaAluno.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPesquisaAlunoActionPerformed(evt);
            }
        });
        btnPesquisaAluno.setBounds(20, 20, 150, 40);
        jLayeredPane3.add(btnPesquisaAluno, javax.swing.JLayeredPane.DEFAULT_LAYER);

        btnIserirAluno.setIcon(new javax.swing.ImageIcon(getClass().getResource("/app/view/gui/imgs/cancelarprof.png"))); // NOI18N
        btnIserirAluno.setText("Excluir Aluno");
        btnIserirAluno.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnIserirAlunoActionPerformed(evt);
            }
        });
        btnIserirAluno.setBounds(200, 20, 140, 40);
        jLayeredPane3.add(btnIserirAluno, javax.swing.JLayeredPane.DEFAULT_LAYER);

        btnCadastrarTurma.setIcon(new javax.swing.ImageIcon(getClass().getResource("/app/view/gui/imgs/images.jpg"))); // NOI18N
        btnCadastrarTurma.setText("Cadastrar Turma");
        btnCadastrarTurma.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCadastrarTurmaActionPerformed(evt);
            }
        });
        btnCadastrarTurma.setBounds(480, 100, 181, 73);
        jLayeredPane3.add(btnCadastrarTurma, javax.swing.JLayeredPane.DEFAULT_LAYER);

        btnCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/app/view/gui/imgs/images (1).jpg"))); // NOI18N
        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });
        btnCancelar.setBounds(480, 190, 180, 73);
        jLayeredPane3.add(btnCancelar, javax.swing.JLayeredPane.DEFAULT_LAYER);

        tblAlunos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Id", "Matrícula", "Nome", "Sobrenome"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane3.setViewportView(tblAlunos);

        jScrollPane3.setBounds(10, 80, 452, 230);
        jLayeredPane3.add(jScrollPane3, javax.swing.JLayeredPane.DEFAULT_LAYER);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLayeredPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 677, Short.MAX_VALUE)
            .addComponent(jLayeredPane2)
            .addComponent(jLayeredPane3)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLayeredPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLayeredPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLayeredPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 362, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void btnCadastrarTurmaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCadastrarTurmaActionPerformed
        if (validaCampos()) {
            this.viewTurma.cadastrarTurma(edtNomeMateria.getText(), this.ProfessorResponsavel,
                    this.AlunosMatriculadosNaTurma);
            JOptionPane.showMessageDialog(this, "Turma Cadastrada com Sucesso.",
                    "Adicionar Turma", JOptionPane.INFORMATION_MESSAGE);
            limpaCampos();
        }
    }//GEN-LAST:event_btnCadastrarTurmaActionPerformed

    private void btnAdicionaProfessorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAdicionaProfessorActionPerformed
        if (tblProfessor.getRowCount() < 1) {
            new ListaProfessores(tblProfessor, ProfessorResponsavel).setVisible(true);
        } else {
            JOptionPane.showMessageDialog(this, "Atenção! Já existe um professor selecionado."
                    + "Por Favor exclua o professor atual para poder selecionar outro.",
                    "Adicionar Professor", JOptionPane.WARNING_MESSAGE);
        }
    }//GEN-LAST:event_btnAdicionaProfessorActionPerformed

    private void btnCancelarProfessorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarProfessorActionPerformed
        if (tblProfessor.getSelectedRow() != -1) {
            DefaultTableModel model = (DefaultTableModel) this.tblProfessor.getModel();
            model.removeRow(tblProfessor.getSelectedRow());
        } else {
            JOptionPane.showMessageDialog(this, "Atenção! Selecione o Professor na tabela para excluí-lo.",
                    "Excluir Professor", JOptionPane.WARNING_MESSAGE);
        }
    }//GEN-LAST:event_btnCancelarProfessorActionPerformed

    private void btnPesquisaAlunoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPesquisaAlunoActionPerformed
        new ListaAlunos(tblAlunos, this.AlunosMatriculadosNaTurma).setVisible(true);
    }//GEN-LAST:event_btnPesquisaAlunoActionPerformed

    private void btnIserirAlunoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnIserirAlunoActionPerformed
        if (tblAlunos.getSelectedRow() != -1) {
            MatriculaAlunoDTO removerMatricula = new MatriculaAlunoDTO();
            DefaultTableModel model = (DefaultTableModel) this.tblAlunos.getModel();
            String Id = String.valueOf(((DefaultTableModel) this.tblAlunos.getModel()).getValueAt(tblAlunos.getSelectedRow(), 0));
            //procurar o aluno que foi selecionado na lista e remove-lo
            for (MatriculaAlunoDTO matricula : this.AlunosMatriculadosNaTurma) {
                if (Integer.valueOf(Id) == matricula.getId()) {
                    removerMatricula = matricula;
                }
            }
            this.AlunosMatriculadosNaTurma.remove(removerMatricula);
            model.removeRow(tblAlunos.getSelectedRow());
        } else {
            JOptionPane.showMessageDialog(this, "Atenção! Selecione um aluno na tabela para excluí-lo.",
                    "Excluir Aluno", JOptionPane.WARNING_MESSAGE);
        }
    }//GEN-LAST:event_btnIserirAlunoActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdicionaProfessor;
    private javax.swing.JButton btnCadastrarTurma;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnCancelarProfessor;
    private javax.swing.JButton btnIserirAluno;
    private javax.swing.JButton btnPesquisaAluno;
    private javax.swing.JTextField edtNomeMateria;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLayeredPane jLayeredPane1;
    private javax.swing.JLayeredPane jLayeredPane2;
    private javax.swing.JLayeredPane jLayeredPane3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTable tblAlunos;
    private javax.swing.JTable tblProfessor;
    // End of variables declaration//GEN-END:variables

    public boolean validaCampos() {
        if (edtNomeMateria.getText().isEmpty()) {
            JOptionPane.showMessageDialog(this, "Atenção! Preencha o campo Nome da matéria.", "Nome da Matéria",
                    JOptionPane.WARNING_MESSAGE);
            return false;
        } else if (tblProfessor.getRowCount() <= 0) {
            JOptionPane.showMessageDialog(this, "Atenção! Adicione um professor para abrir uma turma.",
                    "Adicionar Professor", JOptionPane.WARNING_MESSAGE);
            return false;
        } else if (tblAlunos.getRowCount() <= 0) {
            JOptionPane.showMessageDialog(this, "Atenção! Adicione pelo menos um aluno para abrir uma turma.",
                    "Adionar Aluno", JOptionPane.WARNING_MESSAGE);
            return false;
        } else {
            return true;
        }
    }
    
    public void limpaCampos(){
        this.edtNomeMateria.setText("");
        //this.AlunosMatriculadosNaTurma.removeAll(AlunosMatriculadosNaTurma);
        DefaultTableModel model = (DefaultTableModel) this.tblAlunos.getModel();
        DefaultTableModel model2 = (DefaultTableModel) this.tblProfessor.getModel();
        for(int i = 0; i < this.tblAlunos.getRowCount(); i++){
            model.removeRow(i);
        }
        for(int j = 0; j < this.tblProfessor.getRowCount(); j++){
            model2.removeRow(j);
        }
    } 
}
